package com.academic.system.service.impl;

import com.academic.common.core.domain.entity.SchoolRead;
import com.academic.common.core.domain.entity.SysDepts;
import com.academic.common.core.domain.entity.SysUsers;
import com.academic.common.core.page.TableDataInfo;
import com.academic.system.domain.rep.UserSchoolRep;
import com.academic.system.domain.req.NewReq;
import com.academic.system.mapper.SysUsersMapper;
import com.academic.system.mapper.dao.SchoolReadDao;
import com.academic.system.mapper.dao.SysDeptsDao;
import com.academic.system.service.SchoolReadService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (SchoolRead)表服务实现类
 *
 * @author makejava
 * @since 2024-10-31 22:55:00
 */
@Service("schoolReadService")
public class SchoolReadServiceImpl extends ServiceImpl<SchoolReadDao, SchoolRead> implements SchoolReadService {
    private final SchoolReadDao schoolReadDao;
    private final SysUsersMapper sysUsersMapper;
    private final  SysDeptsDao sysDeptsDao;
    private final  SysBookServiceImpl sysBookService;

    public SchoolReadServiceImpl(SchoolReadDao schoolReadDao, SysUsersMapper sysUsersMapper, SysDeptsDao sysDeptsDao, SysBookServiceImpl sysBookService) {
        this.schoolReadDao = schoolReadDao;
        this.sysUsersMapper = sysUsersMapper;
        this.sysDeptsDao = sysDeptsDao;
        this.sysBookService = sysBookService;
    }

    @Override
    public TableDataInfo getSchoolRead(NewReq newReq) {
        //获取用户的书籍
        PageHelper.startPage(newReq.getPageNum(), newReq.getPageSize());
        List<UserSchoolRep> userSchoolReps =new ArrayList<>();
        QueryWrapper<SchoolRead> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",newReq.getId());
        List<SchoolRead> schoolReads = schoolReadDao.selectList(queryWrapper);
        PageInfo<SchoolRead> pageInfo = new PageInfo<>(schoolReads);
        QueryWrapper<SysUsers> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",newReq.getId());
        SysUsers sysUsers = sysUsersMapper.selectOne(queryWrapper1);
        QueryWrapper<SysDepts> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("dept_id",sysUsers.getDeptId());
        SysDepts sysDept = sysDeptsDao.selectOne(queryWrapper2);
        if (schoolReads != null && !schoolReads.isEmpty()) {
            for (SchoolRead schoolRead : schoolReads) {
                UserSchoolRep userSchoolRep = new UserSchoolRep();
                userSchoolRep.setUserId(newReq.getId());
                userSchoolRep.setReadExam(schoolRead.getReadExam());
                userSchoolRep.setExam(schoolRead.getExam());
                userSchoolRep.setPhone(sysUsers.getPhonenumber());
                userSchoolRep.setReadYears(schoolRead.getReadYears());
                userSchoolRep.setStatus(schoolRead.getStatus());
                userSchoolRep.setStudentName(sysUsers.getNickName());
                userSchoolRep.setCreateTime(schoolRead.getCreateTime());
                userSchoolRep.setClassName(sysDept.getDeptName());
                userSchoolReps.add(userSchoolRep);
            }
        }
        return sysBookService.getDataTable(userSchoolReps,pageInfo.getTotal());
    }
}

